import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class MyCardsPanel extends Panel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	BadugiClient bc;
	private JButton cards[], accept, reject;
	private ImageIcon imgClub, imgDiamond, imgHeart, imgSpade, imgReject, imgAccept, imgCard;
	private int change[], chTime=0, cardRange;
	MyCardsPanel(BadugiClient bc)
	{
		setBackground(new Color(0,172,15));
		this.bc = bc;
		change = new int[4];
		setLayout(new FlowLayout(FlowLayout.CENTER, 10, 4));
		setSize(750, 100);
		setFont(new Font("Lucida Console", Font.PLAIN, 22));
		imgCard = new ImageIcon(getClass().getResource("img/blankCard.png"));
		imgClub = new ImageIcon(getClass().getResource("img/club.png"));
		imgDiamond = new ImageIcon(getClass().getResource("img/diamond.png"));
		imgHeart = new ImageIcon(getClass().getResource("img/heart.png"));
		imgSpade = new ImageIcon(getClass().getResource("img/spade.png"));
		imgReject = new ImageIcon(getClass().getResource("img/reject.png"));
		imgAccept = new ImageIcon(getClass().getResource("img/accept.png"));
		cards = new JButton[4];
		for(int i=0; i<4; i++)
		{
			cards[i] = new JButton("");
			cards[i].setHorizontalTextPosition(SwingConstants.CENTER);
			cards[i].setForeground(Color.WHITE);
			cards[i].setPreferredSize(new Dimension(100, 100));
			cards[i].addActionListener(this);
			change[i]=0;
			cards[i].setIcon(imgCard);
		}
		reject = new JButton("", imgReject);
		reject.setPreferredSize(new Dimension(50, 100));
		reject.addActionListener(this);
		reject.setVisible(false);
		accept = new JButton("", imgAccept);
		accept.setPreferredSize(new Dimension(50, 100));
		accept.addActionListener(this);
		accept.setVisible(false);
		add(reject);
		for(int i=0; i<4; i++) add(cards[i]);
		add(accept);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==accept)
		{
			reject.setVisible(false);
			accept.setVisible(false);
			chTime=0;
			changeRequest(change);
			for(int i=0; i<4; i++)
			{
				change[i]=0;
				cards[i].setEnabled(true);
			}
		}
		else if(e.getSource()==reject)
		{
			for(int i=0; i<4; i++)
			{
				change[i]=0;
				cards[i].setEnabled(true);
			}
		}
		else
		{
			for(int i=0; i<4; i++)
			{
				if(e.getSource()==cards[i] && chTime==1)
				{
					change[i]=1;
					cards[i].setEnabled(false);
				}
			}
		}
	}
	private void changeRequest(int[] change)
	{
		bc.changeRequest(change);
	}
	void changeTime()
	{
		for(int i=0; i<4; i++)
		{
			cards[i].setEnabled(true);
		}
		reject.setVisible(true);
		accept.setVisible(true);
		chTime=1;
	}
	void changeCards(String[] newCards)
	{
		for(int i=0; i<4; i++)
		{
			if(newCards[i].charAt(0)=='0')
			{
				cards[i].setIcon(imgCard);
				cards[i].setText("");
			}
			else
			{
				if(newCards[i].charAt(0)=='C')
				{
					cards[i].setIcon(imgClub);
				}
				else if(newCards[i].charAt(0)=='D')
				{
					cards[i].setIcon(imgDiamond);
				}
				else if(newCards[i].charAt(0)=='H')
				{
					cards[i].setIcon(imgHeart);
				}
				else if(newCards[i].charAt(0)=='S')
				{
					cards[i].setIcon(imgSpade);
				}
				cards[i].setFont(new Font("Lucida Console", 0, 30));
				try
				{
					cardRange=Integer.parseInt(newCards[i].substring(1));
					if(cardRange==1)
					{
						cards[i].setText("A");
					}
					else if(cardRange>=2 && cardRange<=10)
					{
						cards[i].setText(""+cardRange);
					}
					else if(cardRange==11)
					{
						cards[i].setText("J");
					}
					else if(cardRange==12)
					{
						cards[i].setText("Q");
					}
					else if(cardRange==13)
					{
						cards[i].setText("K");
					}
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
			}
		}
	}
}
